package com.ym.student.system.service.impl;

import com.ym.student.system.entity.SysMenu;
import com.ym.student.system.mapper.SysMenuMapper;
import com.ym.student.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author self-DIscipline123
 * @since 2025-04-09
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public List<SysMenu> getUserMenuTree(Integer userId) {
        List<SysMenu> menus = SysMenuMapper.selectMenusUserById(userId);
        return buildMenuTree(menus, 0L);
    }

    private List<SysMenu> buildMenuTree(List<SysMenu> menus, long l) {
        return menus.stream()
                .filter(menu -> menu.equals(menu.getParentId()))
                .peek(menu -> {
                    List<SysMenu> children = buildMenuTree(menus, menu.getMenuId());
                    menu.setChildren(children.isEmpty() ? null : children);
                })
                .collect(Collectors.toList());
    }
    }

